package com.java.star.mall.front.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.java.star.mall.common.constant.web.PageConstant;
import com.java.star.mall.common.response.JsonPage;
import com.java.star.mall.common.response.JsonResult;
import com.java.star.mall.common.valid.InsertGroups;
import com.java.star.mall.common.valid.UpdateGroups;
import com.java.star.mall.front.pojo.param.AddressParam;
import com.java.star.mall.front.pojo.param.UpdateAddressParam;
import com.java.star.mall.front.pojo.param.UserLocationParam;
import com.java.star.mall.front.pojo.vo.AddressVO;
import com.java.star.mall.front.pojo.vo.GeoCoderAddressVO;
import com.java.star.mall.front.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户地址Controller
 */

@Api(value = "address", tags = "地址")
@RestController
@RequestMapping("/address")
@Validated
public class AddressController {

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "根据经纬度获取用户的所在位置")
    @ApiOperationSupport(order = 100)
    @GetMapping("/getUserLocation")
    public JsonResult<GeoCoderAddressVO> getUserLocation(UserLocationParam userLocationParam) {
        GeoCoderAddressVO coderAddressVO = addressService.getUserLocation(userLocationParam);
        return JsonResult.ok(coderAddressVO);
    }

    @ApiOperation(value = "根据地市信息获取用户的所在位置")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "address", value = "地址（地址中需包含省市区信息）", dataType = "string"),
    })
    @GetMapping("/getUserLocationByCityInfo")
    public JsonResult<GeoCoderAddressVO> getUserLocationByCityInfo(String address) {
        GeoCoderAddressVO coderAddressVO = addressService.getUserLocationByCityInfo(address);
        return JsonResult.ok(coderAddressVO);
    }

    @ApiOperation(value = "添加用户收货地址")
    @ApiOperationSupport(order = 300)
    @PostMapping("/insert")
    public JsonResult<Void> addAddress(@Validated(InsertGroups.class) @RequestBody AddressParam addressParam) {
        addressService.insert(addressParam);
        return JsonResult.ok();
    }

    @ApiOperation(value = "分页查询用户收货地址")
    @ApiOperationSupport(order = 400)
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", dataType = "int")
    })
    public JsonResult<JsonPage<AddressVO>> list(
            @RequestParam(required = false, defaultValue = PageConstant.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false, defaultValue = PageConstant.DEFAULT_PAGE_SIZE) Integer pageSize) {
        JsonPage<AddressVO> jsonPage = addressService.list(page, pageSize);
        return JsonResult.ok(jsonPage);
    }

    @ApiOperation(value = "删除收货地址")
    @ApiOperationSupport(order = 500)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressId", value = "地址id", required = true, dataType = "Integer")
    })
    @PostMapping("/delete/{addressId}")
    public JsonResult<Void> delete(@PathVariable Integer addressId) {
        addressService.delete(addressId);
        return JsonResult.ok();
    }

    @ApiOperation(value = "修改地址默认状态")
    @ApiOperationSupport(order = 600)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressId", value = "地址id", required = true, dataType = "Integer")
    })
    @PostMapping("/updateDefault/{addressId}")
    public JsonResult<Void> updateDefault(@PathVariable Integer addressId) {
        addressService.updateDefault(addressId);
        return JsonResult.ok();
    }

    @ApiOperation(value = "根据id查询收货地址信息")
    @ApiOperationSupport(order = 700)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressId", value = "地址id", required = true, dataType = "Integer")
    })
    @GetMapping("/detail/{addressId}")
    public JsonResult<AddressVO> detail(@PathVariable Integer addressId) {
        AddressVO addressVO = addressService.getAddress(addressId);
        return JsonResult.ok(addressVO);
    }

    @ApiOperation(value = "修改用户收货地址")
    @ApiOperationSupport(order = 800)
    @PostMapping("/update")
    public JsonResult<Void> updateAddress(@Validated(UpdateGroups.class) @RequestBody AddressParam addressParam) {
        addressService.update(addressParam);
        return JsonResult.ok();
    }
}
